package managedbean.FRM.CreditAccount;

import entity.CRM.Customer;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;

@ManagedBean(name="addDepositManagerBean")
@ViewScoped
public class AddDepositManagerBean implements Serializable {
    public AddDepositManagerBean() {}
    
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    
    private Customer selectedCustomer;
    private Double depositAdd;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedCustomer==null)
                selectedCustomer = (Customer)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedCustomer");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }

    public Double getDepositAdd() {
        return depositAdd;
    }

    public void setDepositAdd(Double depositAdd) {
        this.depositAdd = depositAdd;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
    
    public void saveNewDeposit(ActionEvent event)
    {
        creditAccountBeanLocal.addDeposit(selectedCustomer, depositAdd);
        statusMessage = "New deposit of $" +this.depositAdd+ " has been added Successfully"; 
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + ". Your credit limit has been updated to $" +customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()).getCreditAccount().getCreditLimit(), "")); 
        
        this.setSelectedCustomer(customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()));
    }
    
}
